package com.aaa.exceptionhandler;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * spring ioc管理 异常处理组件
 */
@ControllerAdvice
public class MyException {

    // 算术异常  除数为0
    // 具体的异常的信息  异常的class对象
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result err(Exception exception){
        System.out.println("-------");
        return new Result(exception.getMessage());
    }

    // NullPointerException


}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Result{
    private Integer code=1;
    private String msg="失败";
    private String exmsg;

    public Result(String exmsg) {
        this.code=1;
        this.msg="失败";
        this.exmsg = exmsg;
    }
}